package com.itheima.springboottilasmanager.survice.deptsurvice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.springboottilasmanager.mapper.DeptMapper;
import com.itheima.springboottilasmanager.mapper.EmpMapper;
import com.itheima.springboottilasmanager.pojo.domain.Dept;
import com.itheima.springboottilasmanager.pojo.domain.Emp;
import com.itheima.springboottilasmanager.survice.DeptSurvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class Depts extends ServiceImpl<DeptMapper,Dept> implements DeptSurvice {
@Autowired
private EmpMapper empMapper;

    @Autowired
    private DeptMapper deptMapper;


    @Override
    public void remove(Integer id) {
        List<Emp> emps = empMapper.ListbyId(id);
        if (emps.size() != 0) {
            throw new RuntimeException("部门有员工，不能删除");
        }
        this.removeById(id);
    }

    @Override
    public Boolean saveByID(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        return this.save(dept);
    }


    @Override
    public List<Dept> findall() {
        List<Dept> list = this.list();
        return list;
    }

    @Override
    public boolean updateDepts(Dept d1) {

        return this.updateById(d1);
    }

    @Override
    public Dept selectCount(Integer id) {

        return (Dept) this.getOne(new QueryWrapper<Dept>().eq("id",id));
    }


//    @Override
//    public boolean addDepts(Dept dept) {
//        dept.setCreateTime(LocalDateTime.now());
//        dept.setUpdateTime(LocalDateTime.now());
//        return deptMapper.insert(dept)>0;
//    }
//
//
////查询待删除的部门是否存在员工
////
////此业务不严谨，应该进行逻辑外键处理
//    @Override
//    public boolean deleteDepts(Integer id) {
//
////   查询是否还有员工     List<Emp> emps= empMapper.selectByDeptId(id);
//        List<Emp> emps= empMapper.ListbyId(id);
////        2.判断是否可以删除 if(emps!=null) throw  expection  ;
//        if(emps!=null && !emps.isEmpty()) {
//            throw new DeptNotAllowDeleteException("此部门有员工，不能删除");
//        }
//        return  deptMapper.deleteById(id)>0;
//    }
//
//
//
//    @Override
//    public List<Dept> findall() {
//   List<Dept> result= deptMapper.selectList(null);
//        return  result;
//    }
//    @Override
//    public boolean updateDepts(Dept dept) {
//dept.setUpdateTime(LocalDateTime.now());
//        return deptMapper.updateById(dept)>0;
//    }
//    @Override
//    public Dept selectCount(Integer id) {
//        Dept result= deptMapper.selectById(id);
//        return  result;
//    }


}
